from django.contrib.auth.models import User
from django.db import models

# Create your models here.

from common.common_models import CommonInfo


class EnvInfo(CommonInfo):
    class Meta:
        verbose_name = "环境表"
        db_table = "env_info"

    project_id = models.BigIntegerField("项目id", null=False)
    name = models.CharField("环境名称", null=False, max_length=30)
    remark = models.CharField("备注", blank=True, null=True, max_length=255)

    def as_dict(self):
        create_time = self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        update_time = self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            "id": self.id,
            "project_id": self.project_id,
            "name": self.name,
            "remark": self.remark,
            "create_time": create_time,
            "update_time": update_time,
            "is_deleted": self.is_deleted
        }


class EnvParams(CommonInfo):
    class Meta:
        verbose_name = "环境参数表"
        db_table = "env_params"

    project_id = models.BigIntegerField("项目id", null=False)
    name = models.CharField("环境参数名称", null=False, max_length=30)
    value = models.TextField("参数值", null=True)

    def as_dict(self):
        create_time = self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        update_time = self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            "id": self.id,
            "project_id": self.project_id,
            "name": self.name,
            "value": self.value,
            "create_time": create_time,
            "update_time": update_time,
            "isDeleted": self.is_deleted
        }


class DBConnection(CommonInfo):
    class Meta:
        verbose_name = "数据库连接数据表"
        db_table = "db_connection"

    DB_TYPE_CHOICES = ((1, "mysql"), (2, "test"))
    project_id = models.BigIntegerField("项目id", null=False)
    db_type = models.IntegerField("环境id", null=False, default=1, choices=DB_TYPE_CHOICES)
    name = models.CharField("数据源名称", null=False, max_length=255)
    db_connection_host = models.CharField("数据源ip地址", null=False, max_length=255)
    db_connection_port = models.IntegerField("数据源端口", null=False, default=3306)
    db_connection_db = models.CharField("数据源连接名称", null=False, max_length=255)
    db_connection_user = models.CharField("数据库连接账户", null=False, max_length=255)
    db_connection_password = models.CharField("数据库连接密码", null=False, max_length=255)


class OperationLog(models.Model):
    ACTION_CHOICES = (
        ('CREATE', '创建'),
        ('UPDATE', '更新'),
        ('DELETE', '删除'),
    )

    action_time = models.DateTimeField('操作时间', auto_now_add=True)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, verbose_name='操作用户')
    content_type = models.ForeignKey('contenttypes.ContentType', on_delete=models.SET_NULL, null=True)
    object_id = models.PositiveIntegerField(null=True)
    object_repr = models.CharField('对象表示', max_length=200)
    action_flag = models.CharField('操作类型', max_length=10, choices=ACTION_CHOICES)
    change_message = models.TextField('变更信息', blank=True)

    class Meta:
        ordering = ['-action_time']
        verbose_name = '操作日志'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.action_time} - {self.user.username}: {self.get_action_flag_display()} {self.object_repr}"
